<template>
  <div class="server-main">
    <div class="cate-head">
      <el-form :inline="true" v-model="modelData" class="search-form">
        <el-form-item label="用例名称">
          <el-input
            class="form-input"
            v-model.trim="modelData.caseName"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="执行结果:" class="status">
          <el-select v-model="modelData.excuteResult">
            <el-option label="全部" value=""></el-option>
            <el-option label="通过" value="1"></el-option>
            <el-option label="未通过" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="缺陷类型:" class="status">
          <el-select v-model="modelData.defectType">
            <el-option label="全部" value=""></el-option>
            <el-option label="功能测试" value="1"></el-option>
            <el-option label="安全性测试" value="2"></el-option>
            <el-option label="性能测试" value="3"></el-option>
            <el-option label="安装性测试" value="4"></el-option>
            <el-option label="高可用性测试" value="5"></el-option>
            <el-option label="兼容性测试" value="6"></el-option>
            <el-option label="接口测试" value="7"></el-option>
            <el-option label="人机交互界面测试" value="8"></el-option>
            <el-option label="文档问题" value="9"></el-option>
            <el-option label="其它问题" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="缺陷严重程度:" class="status">
          <el-select v-model="modelData.defectLevel">
            <el-option label="全部" value=""></el-option>
            <el-option label="一般" value="0"></el-option>
            <el-option label="重要" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="质量类型:" class="status">
          <el-select v-model="modelData.qualityType">
            <el-option label="全部" value=""></el-option>
            <el-option label="功能性" value="功能性"></el-option>
            <el-option label="可靠性" value="可靠性"></el-option>
            <el-option label="易用性" value="易用性"></el-option>
            <el-option label="效率" value="效率"></el-option>
            <el-option label="维护性" value="维护性"></el-option>
            <el-option label="可移植性" value="可移植性"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button id="btn" size="small" type="primary" @click="onSubmit(1)"
            >查询</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <div class="server-table">
      <div class="table-title">
        <span class="table-title-text">质量评估</span>
      </div>
      <el-table :data="tableList" class="km-table">
        <el-table-column
          type="index"
          label="ID"
          width="50"
          align="center"
        ></el-table-column>
        <!-- <el-table-column prop="clusterType"  label="簇" align="center" show-overflow-tooltip v-if="cuVisible"></el-table-column> -->
        <el-table-column
          prop="caseName"
          label="测试用例名称"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="testExplain"
          label="测试说明"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="inputOperationExplain"
          label="输入及操作说明"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="expectTestResult"
          label="期望测试结果"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="actualTestResult"
          label="实际测试结果"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="excuteResultName"
          label="执行结果"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="problemFlag"
          label="问题标识"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="problemDescription"
          label="问题描述"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="defectTypeName"
          label="缺陷类型"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="defectLevelName"
          label="缺陷严重程度"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="implementTime"
          label="执行日期"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="implementUseId"
          label="执行人"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="qualityType"
          label="质量类型"
          align="center"
          show-overflow-tooltip
        ></el-table-column>
      </el-table>
      <el-pagination
        class="km-pagination"
        background
        :small="true"
        layout="total, prev, pager, next,sizes, jumper"
        :total="total"
        :hide-on-single-page="!total"
        :page-size.sync="modelData.pageSize"
        :pager-count="7"
        :page-sizes="[10, 20, 30]"
        :current-page="modelData.pageNum"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      >
      </el-pagination>
    </div>
    <el-dialog
      title="测试结果分析编辑"
      :visible.sync="dialogFormVisible"
      @close="afterDialogClose"
    >
      <el-form
        :model="updateModel"
        ref="dialogForm"
        label-position="right"
        label-width="120px"
        :inline="true"
      >
        <el-form-item label="缺陷类型:" class="status">
          <el-select v-model="updateModel.defectType">
            <el-option label="功能测试" value="1"></el-option>
            <el-option label="安全性测试" value="2"></el-option>
            <el-option label="性能测试" value="3"></el-option>
            <el-option label="安装性测试" value="4"></el-option>
            <el-option label="高可用性测试" value="5"></el-option>
            <el-option label="兼容性测试" value="6"></el-option>
            <el-option label="接口测试" value="7"></el-option>
            <el-option label="人机交互界面测试" value="8"></el-option>
            <el-option label="文档问题" value="9"></el-option>
            <el-option label="其它问题" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="缺陷严重程度:" class="status">
          <el-select v-model="updateModel.defectLevel">
            <el-option label="一般" value="0"></el-option>
            <el-option label="重要" value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetDialogForm">取 消</el-button>
        <el-button type="primary" @click="submitDialogForm" :loading="loading"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapState, mapMutations } from "vuex";
export default {
  name: "OntologyRelationManagement",
  components: {},
  data: () => {
    return {
      isActive: null,
      modelData: {
        resultId: "", // 测试结果id
        caseName: "", //用例名称
        excuteResult: "", //执行结果
        defectType: "", //缺陷类型
        defectLevel: "", //缺陷严重程度
        pageNum: 1, // 页码
        pageSize: 10, // 每页条数
        qualityType: "", //质量类型
      },
      updateModel: {
        detailId: "", //详情id
        defectType: "", //缺陷类型
        defectLevel: "", //缺陷严重程度
      },
      deleteModel: {
        detailId: "",
      },
      cuTotal: 0, //簇的数量
      clusterModel: {
        resultId: "",
        clusterType: "", //簇名
        pageNum: 1, // 页码
        pageSize: 10, // 每页条数
      },
      dialogConfig: {
        ADD: {
          title: "创建本体",
          funcName: "createOntology",
        },
        EDIT: {
          title: "测试结果分析编辑",
          funcName: "editTestResult",
        },
      },
      activeItem: "",
      clusterList: [], //聚类分析的簇的列表
      dialogFormVisible: false,
      cateOptions: [], // 本体模型列表
      ontologyModelId: "", // 本体模型的 id
      tableList: [], // 本体类别列表
      property: [], // 本体类别(节点)的属性列表
      total: 0, // 数据总条数
      loading: false,
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      cuVisible: false,
      classtext: "聚类分析",
      cuModel: {
        resultId: "",
        pageNum: 1, // 页码
        pageSize: 10, // 每页条数
      }, //聚类分析请求模板
      sourceList: [], // 头本体的数据
      targetList: [], // 尾本体的数据
      editOntologyId: null, // 编辑本体类别的id
    };
  },
  created() {
    // 获取结果详情列表
    console.log("t", this.$route.query.resultId);
    this.modelData.resultId = this.$route.query.resultId;
    this.getModelList();
  },
  computed: {
    ...mapState({
      containerDom: (state) => state.server.containerDom,
    }),
  },

  methods: {
    ...mapMutations({
      save_ontologyAllId: "ontologyModel/save_ontologyAllId",
    }),

    // 获取本体模型列表
    getOntologyModelOptions() {
      let params = null;
      return this.$axios.get(this.$api.ontologyModel.xinbiaoObj.searchall, {
        params,
      });
    },
    delete1(id) {
      this.deleteModel.detailId = id;
      this.deleteDetail();
    },
    deleteDetail() {
      let params = this.deleteModel;
      this.$confirm("此操作删除此记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //删除的主体方法
          this.$axios
            .post(this.$api.resultAnalyse.zehaoObj.deleteDetail, params)
            .then((res) => {
              this.getModelList();
            })
            .catch((err) => {
              console.error(err);
            });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    changeCate(id) {
      let obj = this.cateOptions.find((item) => {
        return item.ontologyAllId === id;
      });
      this.ontologyModelId = obj.ontologyAllId;
      this.getModelList();
    },
    cuvisable() {
      this.cuVisible = !this.cuVisible;
      if (this.cuVisible) {
        this.classtext = "取消聚类";
        this.modelData.pageNum = 1;
        this.getCuModelList();
      } else {
        this.classtext = "聚类分析";
        this.modelData.pageNum = 1;
        this.getModelList();
      }
    },
    getCuModelList() {
      this.cuModel.resultId = this.modelData.resultId;
      let params = this.cuModel;
      this.$axios
        .post(this.$api.resultAnalyse.zehaoObj.cumodelDetailList, params)
        .then((res) => {
          let data = res.data.data;
          this.clusterList = data;
          this.$message(
            "聚类算法计算完成，共得到" + this.clusterList.length + "簇"
          );
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 获取本体关系列表
    getModelList() {
      let params = this.modelData;
      this.$axios
        .post(this.$api.resultAnalyse.zehaoObj.modelDetailList, params)
        .then((res) => {
          let data = res.data.data.list;
          this.total = res.data.data.total;
          this.tableList = data;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    //编辑框
    openEditResult(row) {
      let { detailId, defectType, defectLevel } = row;
      this.updateModel.detailId = detailId;
      this.updateModel.defectType = defectType;
      this.updateModel.defectLevel = defectLevel;
      this.dialogFormVisible = true;
      this.dialogType = "EDIT";
    },
    //模拟框关闭后
    afterDialogClose() {
      this.$refs.dialogForm && this.$refs.dialogForm.resetFields();
    },
    //确定
    submitDialogForm() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          console.log("param", JSON.stringify(this.updateModel));
          let func = this.dialogConfig[this.dialogType].funcName;
          this[func]();
          this.dialogFormVisible = false;
        } else {
          return false;
        }
      });
    },
    //取消
    resetDialogForm() {
      this.dialogFormVisible = false;
    },
    //编辑测试结果
    editTestResult() {
      let params = this.updateModel;
      // console.log('edit', params)
      this.loading = true;
      this.$axios
        .post(this.$api.resultAnalyse.zehaoObj.updateDetail, params)
        .then((res) => {
          this.loading = false;
          this.dialogFormVisible = false;
          this.onSubmit();
        })
        .catch((error) => {
          this.loading = false;
        });
      this.getModelList();
    },
    // 查询
    onSubmit(pageNum) {
      this.modelData.pageNum = pageNum ? pageNum : this.modelData.pageNum;
      if (this.cuVisible) {
        this.cuvisable();
      }
      this.getModelList();
    },
    //根据列表点击进行聚类分析
    clickTab(item) {
      console.log("itwwwem", item);
      this.isActive = item;
      this.clusterModel.resultId = this.modelData.resultId;
      this.clusterModel.clusterType = item === "其他" ? null : item;
      this.modelData.pageNum = 1;
      this.clusterModel.pageNum = 1;
      let params = this.clusterModel;
      this.$axios
        .post(this.$api.resultAnalyse.zehaoObj.modelDetailList, params)
        .then((res) => {
          let data = res.data.data.list;
          this.total = res.data.data.total;
          this.tableList = data;
        })
        .catch((err) => {
          console.error(err);
        });
    },
    // 修改当前页码
    handleCurrentChange(page) {
      this.modelData.pageNum = page;
      if (this.cuVisible) {
        this.clusterModel.pageNum = page;
        let params = this.clusterModel;
        this.$axios
          .post(this.$api.resultAnalyse.zehaoObj.modelDetailList, params)
          .then((res) => {
            let data = res.data.data.list;
            this.total = res.data.data.total;
            this.tableList = data;
          })
          .catch((err) => {
            console.error(err);
          });
      } else {
        this.getModelList();
      }
    },
    //修改页面容量
    handleSizeChange(page) {
      this.modelData.pageSize = page;
      this.getModelList();
    },
    // 跳转详情页
    goDetail(item) {
      const { id, name: relationName } = item;
      this.$router.push(
        `/ontologyModel/ontologyRelationProperty/property/${id}/${relationName}`
      );
    },

    // 修改状态
    changeStatus(item) {
      let { id: ontologySpoId, state } = item;
      let params = {
        ontologySpoId,
      };
      if (+state === 0) {
        // 点击禁用时
        this.$confirm("是否禁用当前本体?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.disableSpo(params);
          })
          .catch(() => {});
      } else {
        this.enableSpo(params);
      }
    },

    // 禁用
    disableSpo(params) {
      this.$axios
        .get(this.$api.ontologyModel.xuekuiObj.disableSpo, {
          params,
        })
        .then((res) => {
          if (res.data.code !== 200) {
            this.$message.warning(res.data.message);
            return;
          }
          this.$message.success("状态更改成功");
          this.onSubmit();
        })
        .catch((err) => {
          this.$message.success("状态更改失败");
          console.error(err);
        });
    },

    // 启用
    enableSpo(params) {
      this.$axios
        .get(this.$api.ontologyModel.xuekuiObj.enableSpo, {
          params,
        })
        .then((res) => {
          if (res.data.code !== 200) {
            this.$message.warning(res.data.message);
            return;
          }
          this.$message.success("状态更改成功");
          this.onSubmit();
        })
        .catch((err) => {
          this.$message.success("状态更改失败");
          console.error(err);
        });
    },

    // 打开编辑本体框(获取节点信息)
    openEditOntology(row) {
      console.log("row2", row);
      this.dialogFormVisible2 = true;
      const { id, property, name, sourceName, targetName } = row;
      this.modelParams2.name = name;
      this.modelParams2.sourceName = sourceName;
      this.modelParams2.targetName = targetName;
      this.ontologySpoId = id;
      this.property = property;
    },

    async getParentNodes(code) {
      let params = {
        ontologyAllId: this.ontologyModelId,
        removeId: code === null ? code : this.editOntologyId,
      };
      let res = await this.$axios.get(
        this.$api.ontologyModel.xuekuiObj.getParentNodes,
        {
          params,
        }
      );
      if (res.data.code !== 200) {
        this.$message.error(res.data.message);
      } else {
        this.sourceList = res.data.data;
        this.targetList = res.data.data;
      }
    },

    // 打开新增本体框
    async openCreateRelation() {
      await this.getParentNodes(null);
      this.dialogFormVisible1 = true;
    },

    submitDialogForm2() {
      this.$refs.dialogForm2.validate((valid) => {
        if (valid) {
          this.editRelation();
          this.dialogFormVisible2 = false;
        } else {
          return false;
        }
      });
    },

    // 创建本体类关系
    createRelation() {
      const { name: relationName, sourceName, targetName } = this.modelParams1;
      let params = {
        relationName,
        ontologyAllId: this.ontologyModelId,
        sourceId: sourceName.splice(sourceName.length - 1, 1)[0],
        targetId: targetName.splice(targetName.length - 1, 1)[0],
      };
      this.loading = true;
      this.$axios
        .post(this.$api.ontologyModel.xuekuiObj.addSpo, params)
        .then((res) => {
          if (+res.data.code !== 200) {
            this.$message.warning(res.data.message);
            this.loading = false;
            return;
          }
          this.loading = false;
          this.dialogFormVisible1 = false;
          this.onSubmit();
        })
        .catch((error) => {
          this.loading = false;
        });
    },

    // 编辑本体类别
    editRelation() {
      const { name: relationName } = this.modelParams2;
      let params = {
        ontologyAllId: this.ontologyModelId,
        ontologySpoId: this.ontologySpoId,
        relationName,
      };
      this.loading = true;
      this.$axios
        .post(this.$api.ontologyModel.xuekuiObj.editSpo, params)
        .then((res) => {
          if (+res.data.code !== 200) {
            this.$message.warning(res.data.message);
            this.loading = false;
            return;
          }
          this.loading = false;
          this.dialogFormVisible2 = false;
          this.onSubmit();
        })
        .catch((error) => {
          this.loading = false;
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.server-main {
  position: relative;

  .search-form {
    background: #ffffff;
    border-radius: 4px;
    padding: 16px 32px;
    .el-form-item {
      margin-bottom: 0;
    }

    /deep/ .el-form--inline .el-form-item {
      display: inline-block;
      margin-right: 0;
      vertical-align: top;
    }

    .form-input {
      margin-right: 32px;
      .el-input,
      .el-select {
        ::v-deep .el-input__inner {
          width: 200px;
          height: 32px;
          background: #ffffff;
          border-radius: 2px;
          border: 1px solid #e0e0e0;
        }
      }
    }
  }

  .server-table {
    margin-top: 16px;
    padding: 16px 32px 64px;
    background: #ffffff;
    border-radius: 4px;

    .table-title {
      display: flex;
      justify-content: space-between;
      align-items: center;

      .table-title-text {
        margin: 8px 0;
        font-size: 16px;
        line-height: 16px;
        border-left: 5px solid $--color-primary;
        padding-left: 10px;
      }

      margin-bottom: 10px;
    }
  }

  .ontology-edit {
    padding: 0 10px;
    & + .ontology-edit {
      border-left: 1px solid #dddddd;
    }
  }

  ::v-deep .el-table__append-wrapper {
    padding: 10px;
  }
  .tabs {
    width: 100%;
    border-radius: 2px;
  }
  .tabWrapper {
    display: flex;
    align-items: center;
    background: #fff;
    border-bottom: 1px solid #f2f2f2;

    li {
      width: 60px;
      list-style: none;
      float: left;
      margin-left: 20px;
      font-size: 14px;
      margin: 0;
      text-align: center;
      height: 30px;
      line-height: 30px;
      cursor: pointer;
      border: 1px solid $--color-primary;
    }

    li.active {
      color: white;
      background-color: $--color-primary;
    }

    li.entity,
    li.property {
      border-right: none;
    }
  }

  ::v-deep .el-dialog {
    width: 500px;
    border-radius: 5px;
  }

  .dialog-inline {
    ::v-deep .el-form-item__content {
      width: 300px;
    }

    button.el-button {
      background-color: $--color-primary !important;
      color: #fff !important;
      line-height: 1.2;
    }
  }
}
.el-input,
.el-select {
  ::v-deep .el-input__inner {
    width: 200px;
    height: 32px;
    background: #ffffff;
    border-radius: 2px;
    border: 1px solid #e0e0e0;
  }
}
</style>
